home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 293_01 / angpp.c < prev    next >
C/C++ Source or Header  |  1989-08-23  |  9KB  |  321 lines

  1. /********************  angpp.c  ******************************
  2.  
  3.    Three dimensional surface reconstruction program
  4.  
  5.    This program assumes that the views from the main axis
  6.    directions were done thus an angle view between two main
  7.    axis views can be created from the depth_code viewa(floating
  8.    point). 
  9.  
  10.  Daniel Geist
  11.  Michael W. Vannier
  12.  
  13.  Mallinckrodt Institute of Radiology
  14.  Washington University School of Medicine
  15.  510 S. Kingshighway Blvd.
  16.  St. Louis, Mo. 63110
  17.  
  18.  Note:  This program is intended for the private non-commercial
  19.  use of interested individuals to provide a fuller explanation of
  20.  the algorithms described in "Three dimensional reconstruction in
  21.  Medical Imaging" by D. Geist and M. Vannier.
  22.  
  23.  1988
  24.  *************************************************************/
  25.  
  26. #include <stdio.h>
  27. #include <math.h>
  28. #define FLOAT_LINE 1024
  29. #define PI 3.141592653
  30.  
  31. /* the structure below is the data for a point on the surface projected
  32.    on the view plane                        */
  33. typedef struct DIS_REC {
  34.     float dist;            /*distance from view plane */
  35.     int indXY;             /* index of data on main axis view */
  36.     char XY;               /* Which main axis view obtained from (X or Y)*/
  37. };
  38.  
  39. struct DIS_REC distances[256]; /* one projected line */
  40.  
  41. int NLINES,IXmax,IYmax,xinc,yinc;
  42. int (*xdir)(),(*ydir)();
  43. float THETA,cosTheta,sinTheta,tgnTheta;
  44. float fxbuf[3][256],fybuf[3][256];/* input buffers */
  45.  
  46. /*  output files  */
  47. char fng[13],fnd[13],xfile[13],yfile[13],DR;
  48.  
  49. succ(i)
  50. int i;
  51. {
  52.     return(i==2?0:i+1);
  53. }
  54.  
  55. prev(i)
  56. int i;
  57. {
  58.     return(i==0?2:i-1);
  59. }
  60.  
  61. forward(i)
  62. int i;
  63.     return(i); 
  64. }
  65.  
  66. backward(i,start)
  67. int i;
  68. {
  69.     return(start-i); 
  70. }
  71.  
  72. /* PUTD - fill a DIS_REC with values */
  73. putd(pdis,D,i,xy_sym)
  74. struct DIS_REC *pdis;
  75. float D;
  76. int i,xy_sym;
  77.  
  78.     pdis->dist=D;
  79.     pdis->indXY=i;
  80.     pdis->XY=xy_sym;
  81. }
  82.  
  83. /* take one line from X and Y views and create a projrcted line */
  84. getdistances(linex,liney)
  85. float linex[],liney[];
  86.  
  87.     int i,IND,X,Y;
  88.     float D;
  89.     for(i=0;i<256;i++) distances[i].XY=0;
  90.     /* project Y-data onto image line */
  91.     for(i=0;i<256;i++) if(liney[i]!=256.0){
  92.         X=(*ydir)(i,255);
  93.         IND=IXmax -(X*sinTheta-liney[i]*cosTheta)+0.5;
  94.         D=liney[i]/sinTheta+(X-liney[i]/tgnTheta)*cosTheta;
  95.         if((IND>=0) && (IND<256)){
  96.             if(distances[IND].XY==0)putd(&distances[IND],D,i,1);
  97.             else if(distances[IND].dist>D)putd(&distances[IND],D,i,1);
  98.         }
  99.     }
  100.  
  101.     /*project X-data onto image plane */
  102.     for(i=0;i<256;i++) if(linex[i]!=256.0){
  103.         Y=(*xdir)(i,255);
  104.         IND=IXmax-(linex[i]*sinTheta-Y*cosTheta)+0.5;
  105.         D=Y/sinTheta+(linex[i]-Y/tgnTheta)*cosTheta;
  106.         if((IND>=0) && (IND<256)){
  107.             if(distances[IND].XY==0)putd(&distances[IND],D,i,2);
  108.             else if(distances[IND].dist>D)putd(&distances[IND],D,i,2);
  109.         }
  110.     }
  111.  
  112.     /* fill holes due to low resolution */
  113.     for(i=1;i<255;i++)if( (distances[i].XY==0) && (distances[i+1].XY!=0) &&
  114.         (distances[i-1].XY!=0))
  115.         putd(&distances[i],distances[i-1].dist,
  116.         distances[i-1].indXY,(int)distances[i-1].XY);
  117. }
  118.  
  119. /* returns gradient shade in point given the variations of the surface */
  120. unsigned char grad(x1,x2,y1,y2,z1,z2,x_fac,y_fac,z_fac)
  121. float x1,x2,y1,y2,z1,z2;
  122. int x_fac,y_fac,z_fac;
  123. {
  124.     float gx,gy,gz,G,nx,ny;
  125.     unsigned char gxint;
  126.     /* components of gradient */
  127.     gx=(x2-x1)/x_fac;
  128.     gy=(y2-y1)/y_fac;
  129.     gz=(z1-z2)/z_fac;
  130.     G=sqrt(gx*gx+gy*gy+gz*gz);
  131.     /*compute nx,ny normalized x,y component of gradient */
  132.     nx=gx/G;
  133.     ny=gy/G;
  134.     gxint=255*(nx*cosTheta+ny*sinTheta)+0.5; /*scale gradient shade by 256 */
  135.     return(gxint);
  136. }
  137.  
  138. doline(linex,linex1,linex2,liney,liney1,liney2,z_fac,fg,fd)
  139. float linex[],linex1[],linex2[],liney[],liney1[],liney2[];
  140. int z_fac;
  141. FILE *fg,*fd;
  142.  
  143.     int i;
  144.     unsigned char lined[256],lineg[256];
  145.     /* empty bit on image line */
  146.     for(i=0;i<256;i++)if(distances[i].XY==0)lineg[i]=lined[i]=0;
  147.     else{
  148.         lined[i]=(distances[i].dist<256)?255-distances[i].dist+0.5:0;
  149.         /* bit on image line projected from Y view */
  150.         if(distances[i].XY==1) switch(distances[i].indXY){
  151.         case 0:
  152.             lineg[i]=
  153.                 grad(liney[1]*yinc,liney[0]*yinc,(float)0,(float)2,
  154.             liney1[0],liney2[0],1,2,z_fac);
  155.             break;
  156.         case 255:
  157.             lineg[i]=
  158.                 grad(liney[255]*yinc,liney[254]*yinc,(float)0,(float)2,
  159.             liney1[255],liney2[255],1,2,z_fac);
  160.             break;
  161.         default:
  162.             lineg[i]=
  163.                 grad(liney[distances[i].indXY+yinc],
  164.             liney[distances[i].indXY-yinc],(float)0,(float)2,
  165.             liney1[distances[i].indXY],
  166.             liney2[distances[i].indXY],2,2,z_fac); 
  167.             break;
  168.         }
  169.         /* bit on image line projected from X view */
  170.         else switch(distances[i].indXY){
  171.         case 0:
  172.             lineg[i]=
  173.                 grad((float)0,(float)2,linex[1]*xinc,linex[0]*xinc,
  174.             linex1[0],linex2[0],2,1,z_fac);
  175.             break;
  176.         case 255:
  177.             lineg[i]=
  178.                 grad((float)0,(float)2,linex[255]*xinc,linex[254]*xinc,
  179.             linex1[255],linex2[255],2,1,z_fac);
  180.             break;
  181.         default:
  182.             lineg[i]=
  183.                 grad((float)0,(float)2,linex[distances[i].indXY+xinc],
  184.             linex[distances[i].indXY-xinc],
  185.             linex1[distances[i].indXY],
  186.             linex2[distances[i].indXY],2,2,z_fac);
  187.             break;
  188.         }
  189.     }
  190.  
  191.     fwrite(lineg,1,256,fg);
  192.     fwrite(lined,1,256,fd);
  193. }
  194.  
  195. doviews()
  196. {
  197.     FILE *fg,*fd,*fx,*fy;
  198.     int z,i,j,k,mid;
  199.     mid=1;
  200.     fd=fopen(fnd,"wb");
  201.     fg=fopen(fng,"wb");
  202.     fx=fopen(xfile,"rb");
  203.     fy=fopen(yfile,"rb");
  204.  
  205.     /*read first three lines */
  206.     for(i=0;i<3;i++){
  207.         fseek(fx,(long)FLOAT_LINE*(*ydir)(i,NLINES-1),SEEK_SET);
  208.         fread(fxbuf[i],1,FLOAT_LINE,fx);
  209.         fseek(fy,(long)FLOAT_LINE*(*xdir)(i,NLINES-1),SEEK_SET);
  210.         fread(fybuf[i],1,FLOAT_LINE,fy);
  211.     }
  212.  
  213.     /* do first line (forward differene)*/
  214.     getdistances(fxbuf[0],fybuf[0]);
  215.     doline(fxbuf[0],fxbuf[0],fxbuf[1],fybuf[0],fybuf[0],fybuf[1],1,fg,fd);
  216.  
  217.     /* do middle lines (central diffrence) */
  218.     printf("Begining computation of  views\n");
  219.     for(z=0;z<(NLINES-2);z++){      /*for each slice */
  220.         getdistances(fxbuf[mid],fybuf[mid]);
  221.         doline(fxbuf[mid],fxbuf[prev(mid)],fxbuf[succ(mid)],
  222.         fybuf[mid],fybuf[prev(mid)],fybuf[succ(mid)],2,fg,fd);
  223.         fseek(fx,(long)FLOAT_LINE*(*ydir)(i,NLINES-1),SEEK_SET);
  224.         fseek(fy,(long)FLOAT_LINE*(*xdir)(i,NLINES-1),SEEK_SET);
  225.         i++;
  226.         fread(fxbuf[prev(mid)],1,FLOAT_LINE,fx);
  227.         fread(fybuf[prev(mid)],1,FLOAT_LINE,fy);
  228.         mid=succ(mid);
  229.         printf(" did %d \n",z);
  230.     }
  231.  
  232.     /* do last line (backward difference)*/
  233.     getdistances(fxbuf[mid],fybuf[mid]);
  234.     doline(fxbuf[mid],fxbuf[prev(mid)],fxbuf[mid],
  235.     fybuf[mid],fybuf[prev(mid)],fybuf[mid],1,fg,fd);
  236.  
  237.     fclose(fg);
  238.     fclose(fd);
  239.     fclose(fx);
  240.     fclose(fy);
  241. }
  242.  
  243. getang(ang)
  244. int *ang;
  245.     scanf("%d",ang);
  246.     sprintf(fng,"gang%d.out",*ang);
  247.     sprintf(fnd,"dang%d.out",*ang);
  248.     if( ( *ang > 0) && ( *ang < 90) ){
  249.         xdir=forward;
  250.         ydir=forward;
  251.         sprintf(xfile,"%c:xdis1.dat",DR);
  252.         sprintf(yfile,"%c:ydis1.dat",DR);
  253.         xinc=1;
  254.         yinc=1;
  255.     }
  256.     else if( ( *ang > 90) && ( *ang < 180) ){
  257.         xdir=backward;
  258.         ydir=forward;
  259.         sprintf(xfile,"%c:ydis1.dat",DR);
  260.         sprintf(yfile,"%c:xdis2.dat",DR);
  261.         xinc=-1;
  262.         yinc=1;
  263.         *ang-=90;
  264.     }
  265.     else if( ( *ang > 180) && ( *ang < 270) ){
  266.         xdir=backward;
  267.         ydir=backward;
  268.         sprintf(xfile,"%c:xdis2.dat",DR);
  269.         sprintf(yfile,"%c:ydis2.dat",DR);
  270.         xinc=-1;
  271.         yinc=-1;
  272.         *ang-=180;
  273.     }
  274.     else if( ( *ang > 270) && ( *ang < 360) ){
  275.         xdir=forward;
  276.